<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-permission="['advertType:add']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button v-permission="['advert:list']" class="filter-item" icon="el-icon-link" type="success" plain @click="toAdvert">广告列表</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中..." border fit highlight-current-row>
      <el-table-column align="center" width="80" label="ID" prop="id" />
      <el-table-column align="center" label="名称" prop="name" />
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['advertType:detail']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['advertType:delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-permission="['advertType:list']" v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="initList"/>

    <el-dialog :close-on-click-modal="false" :title="isAdd?'新增':'编辑'" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="id" prop="id" hidden>
          <el-input v-model="dataForm.id" />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-permission="['advertType:add']" v-if="isAdd" :loading="submitIng" type="primary" @click="createData">确定</el-button>
        <el-button v-permission="['advertType:edit']" v-else :loading="submitIng" type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { AdvertService } from '@/api/PromotionService'
import Pagination from '@/components/Pagination'
import Notify from '@/utils/notify'

export default {
  name: 'Advert',
  components: { Pagination },
  data() {
    return {
      isAdd: true, list: [], total: 0, listLoading: false, listQuery: { pageNo: 1, pageSize: 20 }, dataForm: { id: undefined, name: undefined }, dialogFormVisible: false,
      rules: {
        name: [{ required: true, message: '类型名称不能为空', trigger: 'blur' }]
      },
      submitIng: false
    }
  },
  created() {
    this.initList()
  },
  methods: {
    initList() {
      this.listLoading = true
      AdvertService.listAdvertType(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    resetForm() { this.dataForm = { id: undefined, name: undefined } },
    toAdvert() {
      this.$router.replace({ path: '/promotion/advert' })
    },
    handleCreate() {
      this.resetForm()
      this.dialogFormVisible = true
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submitIng = true
          AdvertService.addAdvertType(this.dataForm).then(() => {
            this.initList()
            this.dialogFormVisible = false
            this.submitIng = false
            Notify.success(this, '新增成功')
          })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogFormVisible = true
      this.isAdd = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          AdvertService.editAdvertType(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.initList()
            Notify.success(this, '更新成功')
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('您确认要删除该类型吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        AdvertService.deleteAdvertType(row.id, row.type).then(() => {
          this.initList()
          Notify.success(this, '删除成功')
        })
      }).catch(() => {
        return false
      })
    }
  }
}
</script>
